﻿using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Web.Http;
using System.Web.Http.Description;
using ClassLibrary1;
using Domain.Entities;

namespace Webservice.Controllers
{
    public class BillStatusController : ApiController
    {
        private readonly MyHealthDbContext db = new MyHealthDbContext();

        // GET: api/BillStatus
        public IQueryable<BillStatus> GetBillStatus()
        {
            return db.BillStatus;
        }

        // GET: api/BillStatus/5
        [ResponseType(typeof(BillStatus))]
        public IHttpActionResult GetBillStatus(int id)
        {
            BillStatus billStatus = db.BillStatus.Find(id);
            if (billStatus == null)
            {
                return NotFound();
            }

            return Ok(billStatus);
        }

        // PUT: api/BillStatus/5
        [ResponseType(typeof(void))]
        public IHttpActionResult PutBillStatus(int id, BillStatus billStatus)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != billStatus.Id)
            {
                return BadRequest();
            }

            db.Entry(billStatus).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BillStatusExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

        // POST: api/BillStatus
        [ResponseType(typeof(BillStatus))]
        public IHttpActionResult PostBillStatus(BillStatus billStatus)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.BillStatus.Add(billStatus);
            db.SaveChanges();

            return CreatedAtRoute("DefaultApi", new { id = billStatus.Id }, billStatus);
        }

        // DELETE: api/BillStatus/5
        [ResponseType(typeof(BillStatus))]
        public IHttpActionResult DeleteBillStatus(int id)
        {
            BillStatus billStatus = db.BillStatus.Find(id);
            if (billStatus == null)
            {
                return NotFound();
            }

            db.BillStatus.Remove(billStatus);
            db.SaveChanges();

            return Ok(billStatus);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool BillStatusExists(int id)
        {
            return db.BillStatus.Count(e => e.Id == id) > 0;
        }
    }
}